/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.galbanie.classes;

import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author galbanie
 */
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher requestD;
        String requete = "";
        String user = request.getParameter("user") != null ? (String)request.getParameter("user") : "";
        String pwd = request.getParameter("pwd") != null ? (String)request.getParameter("pwd") : "";
        if(user.isEmpty() || pwd.isEmpty()){
            requestD = request.getRequestDispatcher("/index.jsp");
            request.setAttribute("error","Saissisez l'utilisateur ou le mot de passe.");
            requestD.forward(request, response);
            return;
        }
        else{
            requete = "SELECT * FROM user WHERE numid = '" + user +"'";
        }
        try{
            // Chargement du driver mysql
            Class.forName("com.mysql.jdbc.Driver");
            // Creation de la connexion
            Connection connexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/ecole", "galbanie", "carole225");
            // creation de la requete
            Statement statement = connexion.createStatement();
            // execution de la requete
            ResultSet resultat = statement.executeQuery(requete);
            
            if(resultat.next()){
                if(resultat.getString("mdp").equals(pwd)){
                    HttpSession session = request.getSession(true);
                    session.setAttribute("user", user);
                }
                else{
                    request.setAttribute("error","Mot de passe incorrect.");
                }
            }
            else{
                request.setAttribute("error","utilisateur incorrect.");
            }
            requestD = request.getRequestDispatcher("/index.jsp");
            requestD.forward(request, response);
        }catch(Exception e){
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
